﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Data;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.ServiceModel;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using System.Collections.ObjectModel;
using KMT_Pro.Web;
using KMT_Pro.Views.ClientViews;
using SilverlightCommands;

namespace KMT_Pro.ViewModels.ClientViewModels
{
    public class ClientMasterViewModel : MessageSendableViewModel
    {
        #region Constants and Fields

        CustomerContext _context;

        private ObservableCollection<SP_ClientMaster_Select_Result> ClientList = new ObservableCollection<SP_ClientMaster_Select_Result>();

        private PagedCollectionView clientCollection;

        private SP_ClientMaster_Select_Result selectedClientMaster;

        //검색어
        public string keyWord = string.Empty;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// 생성자
        /// </summary>
        public ClientMasterViewModel()
        {
            this.SearchClick = new RelayCommand((p) =>
            {
                try
                {
                    _context = new CustomerContext();
                    this.ServerRun(_context.Load(_context.GetClientMaster_SelectQuery(this.KeyWord.Trim(), ""))
                        , (sender, e) =>
                        {
                            //비동기 이벤트 등록
                            try
                            {
                                ClientList.Clear();
                                LoadOperation lo = sender as LoadOperation;
                                foreach (SP_ClientMaster_Select_Result GAP in lo.AllEntities)
                                {
                                    ClientList.Add(GAP);
                                }
                                ClientCollection = new PagedCollectionView(this.ClientList);
                            }
                            catch (Exception)
                            { }
                        });
                }
                catch (Exception)
                { }
            });

            this.editClick = new RelayCommand((p) =>
            {
                try
                {
                    ChildWindow ClientEditInfoView = new ClientEditInfoView(this.SelectedClientMaster.CompanyID.Trim()
                                                                            , this.SelectedClientMaster.CompanyName.Trim()
                                                                            , this.SelectedClientMaster.Address.Trim()
                                                                            , this.SelectedClientMaster.gubun.Trim()
                                                                            , this.SelectedClientMaster.etc.Trim()
                                                                            , true);
                    ClientEditInfoView.Closed += new EventHandler(ClientEditInfoView_Closed);
                    ClientEditInfoView.Show();
                }
                catch (Exception)
                { }
            });

            this.delClick = new RelayCommand((p) =>
            {
                try
                {
                    if (MessageBox.Show(this.SelectedClientMaster.CompanyName.Trim() + " => 정말 삭제하시겠습니까?", "경고!", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                    {
                        return;
                    }


                    _context = new CustomerContext();

                    this.ServerRun(_context.Load(_context.ClientMaster_DeleteQuery(this.SelectedClientMaster.CompanyID.Trim()))
                        , (sender, e) =>
                                {
                                    //비동기 이벤트 등록
                                    LoadOperation lo = sender as LoadOperation;
                                    try
                                    {
                                        foreach (ResultMessage GAP in lo.AllEntities)
                                        {
                                            MessageBox.Show(GAP.ExceptionMessage);
                                            this.SearchClick.Execute(null);
                                        }
                                    }
                                    catch (Exception)
                                    { }
                                });
                }
                catch (Exception ex)
                {
                    ChildWindow errorWin = new ErrorWindow(ex);
                    errorWin.Show();
                }
            });

            this.NewClick = new RelayCommand((p) =>
            {
                try
                {
                    ChildWindow ClientEditInfoView = new ClientEditInfoView("", "", "", "", "", false);
                    ClientEditInfoView.Closed += new EventHandler(ClientEditInfoView_Closed);
                    ClientEditInfoView.Show();
                }
                catch (Exception)
                { }
            });

            this.ExcelCommand = new RelayCommand((p) =>
                {
                    
                });
            //로드시 검색
            this.SearchClick.Execute(null);
        }

        #endregion

        #region Properties

        // ------------------------ 속성 : Start -----------------------------------

        public PagedCollectionView ClientCollection
        {
            set
            {
                this.clientCollection = value;
                this.OnPropertyChanged("ClientCollection");
            }

            get
            {
                return clientCollection;
            }

        }

        public SP_ClientMaster_Select_Result SelectedClientMaster
        {
            set
            {
                this.selectedClientMaster = value;
                this.OnPropertyChanged("SelectedClientMaster");
            }

            get
            {
                return selectedClientMaster;
            }

        }

        public string KeyWord
        {
            set
            {
                this.keyWord = value;
                this.OnPropertyChanged("KeyWord");
            }

            get
            {
                return keyWord;
            }

        }

        // ------------------------ 속성 : End -------------------------------------

        //------------------------- 버튼 : Start -----------------------------------

        public ICommand SearchClick { get; set; }

        public ICommand editClick { get; set; }

        public ICommand delClick { get; set; }

        public ICommand NewClick { get; set; }

        public ICommand ExcelCommand { get; set; }

        //------------------------- 버튼 : End -------------------------------------
        
        #endregion

        #region Event

        void ClientEditInfoView_Closed(object sender, EventArgs e)
        {
            try
            {
                ClientEditInfoView lw = (ClientEditInfoView)sender;

                if (lw.DialogResult == true)
                {
                    this.SearchClick.Execute(null);
                }
            }
            catch(Exception)
            { }
        }

        #endregion          

        #region Method
        /// <summary>
        /// Gets ExcelOutProp.
        /// </summary>
        public PagedCollectionView ExcelOutProp
        {
            get
            {
                return new PagedCollectionView(this.ClientList);
            }
        }
        #endregion
    }
}
